$(function () {
    let markId = getUrlString('markid');
    let sendCardName = '',name = '',userid = '',userName = '',isAjax = true;
    let cardList = [
        {name:'粽叶',sendCardName:'shu',pic:'https://api.odancool.com/weixinapp/res/img/my/cardCenter/zy.jpg',num:'0',
            getInfo:'1、获取普通扭蛋券。</br>2、使用普通扭蛋券在端午集卡蛋机抽取粽子材料卡片。'},
        {name:'糯米',sendCardName:'niu',pic:'https://api.odancool.com/weixinapp/res/img/my/cardCenter/nm.jpg',num:'0',
            getInfo:'1、获取普通扭蛋券。</br>2、使用普通扭蛋券在端午集卡蛋机抽取粽子材料卡片。'},
        {name:'粽绳',sendCardName:'hu',pic:'https://api.odancool.com/weixinapp/res/img/my/cardCenter/zs.jpg',num:'0',
            getInfo:'1、获取普通扭蛋券。</br>2、使用普通扭蛋券在端午集卡蛋机抽取粽子材料卡片。'},
        {name:'猪肉',sendCardName:'tu',pic:'https://api.odancool.com/weixinapp/res/img/my/cardCenter/zr.jpg',num:'0',
            getInfo:'1、获取普通扭蛋券。</br>2、使用普通扭蛋券在端午集卡蛋机抽取粽子材料卡片。'},
        {name:'红豆',sendCardName:'long',pic:'https://api.odancool.com/weixinapp/res/img/my/cardCenter/hd.jpg',num:'0',
            getInfo:'1、获取普通扭蛋券。</br>2、使用普通扭蛋券在端午集卡蛋机抽取粽子材料卡片。'},
        {name:'蛋黄',sendCardName:'she',pic:'https://api.odancool.com/weixinapp/res/img/my/cardCenter/dh.jpg',num:'0',
            getInfo:'1、获取普通扭蛋券。</br>2、使用普通扭蛋券在端午集卡蛋机抽取粽子材料卡片。'},
        {name:'栗子',sendCardName:'ma',pic:'https://api.odancool.com/weixinapp/res/img/my/cardCenter/lz.jpg',num:'0',
            getInfo:'1、获取普通扭蛋券。</br>2、使用普通扭蛋券在端午集卡蛋机抽取粽子材料卡片。'},
        {name:'蜜枣',sendCardName:'yang',pic:'https://api.odancool.com/weixinapp/res/img/my/cardCenter/mz.jpg',num:'0',
            getInfo:'1、获取普通扭蛋券。</br>2、使用普通扭蛋券在端午集卡蛋机抽取粽子材料卡片。'},
        // {name:'猴',sendCardName:'hou',pic:'https://api.odancool.com/weixinapp/res/img/my/cardCenter/9.png',num:'0',
        //     getInfo:'1、参与新春牛扭乐活动抽奖有概率获得。</br>2、蛋机扭蛋分享后有概率获得。</br>3、App充值有概率获得。</br>4、集换卡中心银币兑换可得。</br>5、活动期间连续签到7天有概率获得。'},
        // {name:'鸡',sendCardName:'ji',pic:'https://api.odancool.com/weixinapp/res/img/my/cardCenter/10.png',num:'0',
        //     getInfo:'1、参与新春牛扭乐活动抽奖有概率获得。</br>2、蛋机扭蛋分享后有概率获得。</br>3、App充值有概率获得。</br>4、集换卡中心银币兑换可得。</br>5、活动期间连续签到7天有概率获得。'},
        // {name:'狗',sendCardName:'gou',pic:'https://api.odancool.com/weixinapp/res/img/my/cardCenter/11.png',num:'0',
        //     getInfo:'1、参与新春牛扭乐活动抽奖有概率获得。</br>2、蛋机扭蛋分享后有概率获得。</br>3、App充值有概率获得。</br>4、集换卡中心银币兑换可得。</br>5、活动期间连续签到7天有概率获得。'},
        // {name:'猪',sendCardName:'zhu',pic:'https://api.odancool.com/weixinapp/res/img/my/cardCenter/12.png',num:'0',
        //     getInfo:'1、参与新春牛扭乐活动抽奖有概率获得。</br>2、蛋机扭蛋分享后有概率获得。</br>3、App充值有概率获得。</br>4、集换卡中心银币兑换可得。</br>5、活动期间连续签到7天有概率获得。'},
        ];
    //获取用户信息
    function getUserInfo() {
        getData('post','/niu/apiv3/member_userinfo.php',{'markid':markId},function (data) {
            console.log(data);
            userid = data.data.id;
            userName = data.data.nick;
        })
    }
    getUserInfo();
    function myCardList (MyCards) {
        let data = {
            'markid':markId,
            'method':MyCards
        };
        getData('post','/niu/apiv4/niu_mycards_recordV2.php',data,function (data) {
            console.log(data);
            if (data.data != null){
                cardList[0].num = data.data.shu_card;
                cardList[1].num = data.data.niu_card;
                cardList[2].num = data.data.hu_card;
                cardList[3].num = data.data.tu_card;
                cardList[4].num = data.data.long_card;
                cardList[5].num = data.data.she_card;
                cardList[6].num = data.data.ma_card;
                cardList[7].num = data.data.yang_card;
                // cardList[8].num = data.data.hou_card;
                // cardList[9].num = data.data.ji_card;
                // cardList[10].num = data.data.gou_card;
                // cardList[11].num = data.data.zhu_card;
            }
            let append = '';
            $(cardList).each(function () {

                append += '<li class="CardLi">' +
                    '            <input type="hidden" class="getInfo" value="'+this.getInfo+'">'+
                    '            <input type="hidden" class="sendCardName" value="'+this.sendCardName+'">'+
                    '            <input type="hidden" class="name" value="'+this.name+'">'+
                    '            <div class="num">'+this.num+'</div>' +
                    '            <div class="pics">' ;
                    if (this.num === '0'){
                        append += '                <div class="noCard"></div>';
                    }
                append +='                <img src="'+this.pic+'" alt="">' +
                    '            </div>' +
                    '            <div class="name">'+this.name+'</div>' +
                    '        </li>'
            });
            $('#cardList').empty().append(append);
            $('#cardList .CardLi').click(function () {
                let pic = $(this).find('.pics').find('img').attr('src');
                let getInfo = $(this).find('.getInfo').val();
                sendCardName = $(this).find('.sendCardName').val();
                name = $(this).find('.name').val();
                let num = $(this).children('.num').html();
                if (num === '0'){
                    $('.sendAPP,.sendWX').hide()
                    $('.gotochouniudan').show()
                }else {
                    $('.gotochouniudan').hide()
                    $('.sendAPP,.sendWX').show()
                }
                $('.indexAlert .cardPic img').attr('src',pic);
                $('.indexAlert .getInfo').html(getInfo);
                $('.indexAlert .name').html(name);
                $('.indexAlert').show();
            });
            $('.closeAlert').click(function () {
                $('.indexAlert').hide();
            });

            $('.sendAPP').click(function () {
                $('.indexSessionList').show();
            });

            $('.gotochouniudan').click(function () {
                window.location.href = 'dancool://fullweb='+urlView+'danji/DWCupDanji.html?danjiid=14855'
            });

            $('.sendWX').click(function () {
                // nativeCommand('dancool://wxsapp=gh_f8a2824c3978|||pages/index/index')
                if (isAjax){
                    isAjax = false;
                    let cardPic = $(this).parent().prevAll('.cardPic').find('img').attr('src');
                    firstCard(cardPic)
                }
            })
        })
    }
    myCardList('MyCards');
    function firstCard (cardPic) {
        let data = {
            'userid':userid,
            'card':sendCardName
        };
        getData('post','/niu/apiv4/niu_gift_card_first.php',data,function (data) {
            console.log(data);
            isAjax = true;
            if (data.resultcode === '0'){
                let only_code = data.only_code;
                // console.log(only_code);
                // console.log(sendCardName);
                // console.log(name);
                // console.log(cardPic);
                // console.log(userid);
                // console.log(userName);
                // nativeCommand('dancool://sharewxsappv2=0|||gh_f8a2824c3978|||pages/activity/getCard/getCard?only_code='+only_code+'&sendCardName='+sendCardName+'&name='+name+'&cardPic='+cardPic+'&userid='+userid+'&userName='+userName+'|||'+userName+'赠送给你一张蛋酱新春集换“'+name+'”卡，收集兑换千元大奖|||https://api.odancool.com/weixinapp/res/img/wxsapp/sendCard.jpg|||赠送卡片');
                nativeCommand('dancool://sharewxsappv2=0|||gh_f8a2824c3978|||pages/activity/getCard/getCard?only_code='+only_code+'&sendCardName='+sendCardName+'&name='+name+'&cardPic='+cardPic+'&userid='+userid+'&userName='+userName+'|||'+userName+'赠送给你一张蛋酱端午集换“'+name+'”卡，收集兑换好礼|||https://api.odancool.com/weixinapp/res/danji/img/DWCupDanji/DWShare.jpg|||赠送卡片');
            } else {
                laymsg(data.msg)
            }

        })
    }

    function getFrdList () {
        getData('post','/niu/apiv3/weibo_friend.php?method=getFriendList',{'markid':markId},function (data) {
            console.log(data);
            if (data.datacount === '0'){
                $('#noFrd').show()
            } else {
                $('#noFrd').hide();
                let append = '';
                $(data.data).each(function () {
                    append += ' <li>' +
                        '<input type="hidden" class="name" value="'+this.frienduserid+'">'+
                        '            <div class="left">' +
                        '                <img src="'+this.faceurl+'" alt="">' +
                        '            </div>' +
                        '            <div class="right">' ;
                    if(this.markusername === ''){
                        append+= '                <span>'+this.nick+'</span>' ;
                    }else {
                        append+= '                <span>'+this.markusername+'</span>' ;
                    }
                    append+= '            </div>' +
                        '        </li>'
                });
                $('#conversationList').empty().append(append);
                let userid = '';
                $('#conversationList li').click(function () {
                    let name = $(this).find('.right').find('span').html();
                    userid = $(this).find('input').val();
                    $('.alert .info').html('您确定赠送该卡片给'+name+'吗？');
                    $('.alert').show()
                });
                $('#cancel').click(function () {
                    $('.alert').hide()
                });
                $('#confirm').click(function () {
                    sendCard(sendCardName,userid)
                })
            }
        })
    }

    //获取自己的好友列表
    getFrdList();

    //赠送给好友卡片
    function  sendCard (card,userid){
        let data = {
            'markid':markId,
            'isApp':'0',
            'card':card,
            'userid':userid
        };
        console.log(data);
        getData('post','/niu/apiv4/niu_gift_card_user2.php',data,function (data) {
            if (data.resultcode === '0'){
                laymsg(data.msg);
                $('.alert,.indexSessionList,.indexAlert').hide();
                myCardList('MyCards');
            } else {
                $('.alert').hide();
                laymsg(data.msg)
            }
        })
    }

    $('.close').click(function () {
        $('.indexSessionList').hide();
    })
});
